


using FunctionScope.Models;
using System;
using System.IO;
using System.Windows.Threading;
using System.Xml.Serialization;
using System.Collections;
using System.Collections.Generic;

namespace FunctionScope.ViewModels
{

    public class Trace2DViewModel
    {
        public Trace2DViewModel()
        {
            //Construct models
            _plotSettings = new Trace2DPlotSettingsModel()
            {
                PersistenceMS = 500,
                ShowGraticule = true
            };

            _plotSettings.PropertyChanged += PlotSettings_PropertyChanged;

            _xFunction = new SinFunctionModel()
            {
                Amplitude = 1d,
                Phase = 0d,
                Frequency = 1d,
                DcOffset = 0d,
                Name = "X Signal"
            };

            _yFunction = new SinFunctionModel()
            {
                Amplitude = 1d,
                Phase = 0d,
                Frequency = 1d,
                DcOffset = 0d,
                Name = "Y Signal"
            };

            Sampler = new FunctionSamplerModel(_xFunction, _yFunction);

            //TODO: Once there are multiple traces, split this out from the sampler
            PlotSettings.Trace = Sampler.Trace;

            //Setup Sampler with correct trace size
            SyncPersistenceSetting();

            _presets = new List<PresetState>();

        }


        #region Model Accessors
        public TraceModel<double> Data {
            get
            {
                return Sampler.Trace;
            }
        }

        private SinFunctionModel _xFunction;
        public SinFunctionModel XFunction
        {
            get { return _xFunction; }
        }

        private SinFunctionModel _yFunction;
        public SinFunctionModel YFunction
        {
            get { return _yFunction; }
        }

        public Trace2DPlotSettingsModel _plotSettings;
        public Trace2DPlotSettingsModel PlotSettings
        {
            get { return _plotSettings; }
        }

        public FunctionSamplerModel Sampler;

        private List<PresetState> _presets;
        public List<PresetState> Presets
        {
            get { return _presets; }
        }
        #endregion



        #region Model property changed handlers
        private void PlotSettings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PersistenceMS")
            {
                SyncPersistenceSetting();
            }
        }

        private void SyncPersistenceSetting()
        {
            PlotSettings.PersistenceMS = Sampler.SetTraceSize(PlotSettings.PersistenceMS, 16);
        }

        #endregion

    }




}